package com.frank.fresh_mall.controller;

import com.frank.fresh_mall.common.ApiRestResponse;
import com.frank.fresh_mall.common.Constant;
import com.frank.fresh_mall.exception.FreshMallExceptionEnum;
import com.frank.fresh_mall.model.pojo.Category;
import com.frank.fresh_mall.model.pojo.User;
import com.frank.fresh_mall.model.request.AddCategoryReq;
import com.frank.fresh_mall.model.request.UpdateCategoryReq;
import com.frank.fresh_mall.model.vo.CategoryVO;
import com.frank.fresh_mall.service.CategoryService;
import com.frank.fresh_mall.service.UserService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.List;

/**
 * 目录Controller
 */
@Api("目录")
@Controller
public class CategoryController {
    @Resource
    private UserService userService;

    @Resource
    private CategoryService categoryService;

    @ApiOperation("后台添加分类")
    @PostMapping("/admin/category/add")
    @ResponseBody
    public ApiRestResponse addCategory(HttpSession session,
                                       @Valid @RequestBody AddCategoryReq addCategoryReq) {
        /*if (addCategoryReq.getName() == null ||
                addCategoryReq.getType() == null ||
                addCategoryReq.getOrderNum() == null ||
                addCategoryReq.getParentId() == null) {
            return ApiRestResponse.error(FreshMallExceptionEnum.NEED_ADMIN);
        }*/
        User currentUser = (User) session.getAttribute(Constant.FRESH_MALL_USER);
        if (currentUser == null) { // 没有登录
            return ApiRestResponse.error(FreshMallExceptionEnum.NEED_LOGIN);
        }
        if (!userService.checkAdminRole(currentUser)) { // 不是管理员
            return ApiRestResponse.error(FreshMallExceptionEnum.NEED_ADMIN);
        }
        categoryService.add(addCategoryReq);
        return ApiRestResponse.success();
    }

    @ApiOperation("后台更新分类")
    @PostMapping("/admin/category/update")
    @ResponseBody
    public ApiRestResponse updateCategory(HttpSession session,
                                          @Valid @RequestBody UpdateCategoryReq updateCategoryReq) {
        User currentUser = (User) session.getAttribute(Constant.FRESH_MALL_USER);
        if (currentUser == null) { // 没有登录
            return ApiRestResponse.error(FreshMallExceptionEnum.NEED_LOGIN);
        }
        if (!userService.checkAdminRole(currentUser)) { // 不是管理员
            return ApiRestResponse.error(FreshMallExceptionEnum.NEED_ADMIN);
        }
        Category category = new Category();
        BeanUtils.copyProperties(updateCategoryReq, category);
        categoryService.update(category);
        return ApiRestResponse.success();
    }

    @ApiOperation("后台删除分类")
    @PostMapping("/admin/category/delete")
    @ResponseBody
    public ApiRestResponse deleteCategory(@RequestParam("id") Integer id) {
        categoryService.delete(id);
        return ApiRestResponse.success();
    }

    @ApiOperation("后台分类列表")
    @PostMapping("/admin/category/list")
    @ResponseBody
    public ApiRestResponse listCategoryForAdmin(@RequestParam("pageNum") Integer pageNum,
                                                @RequestParam("pageSize") Integer pageSize) {
        PageInfo<Category> categoryPageInfo = categoryService.listForAdmin(pageNum, pageSize);
        return ApiRestResponse.success(categoryPageInfo);
    }

    @ApiOperation("前台分类列表")
    @GetMapping("/category/list")
    @ResponseBody
    public ApiRestResponse listCategory() {
        List<CategoryVO> categoryVOList = categoryService.listCategoryForCustomer(0);

        return ApiRestResponse.success(categoryVOList);
    }

}
